//
//  HttpRequest.m
//  网络请求1116Demo
//
//  Created by 铅笔 on 15/11/16.
//  Copyright © 2015年 铅笔. All rights reserved.
//

#import "HttpRequest.h"


@interface HttpRequest ()

@end


@implementation HttpRequest

static HttpRequest * webUtil = nil;

+ (HttpRequest *) shardWebUtil
{
    @synchronized([HttpRequest class])
    {
        if (!webUtil) {
            webUtil = [[[self class] alloc] init];
        }
        return webUtil;
    }
    return nil;
}

#pragma mark 检测网路状态
+ (void)netWorkStatus
{
    /**
     AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G 花钱
     AFNetworkReachabilityStatusReachableViaWiFi = 2,   // WiFi
     */
    // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"%ld", (long)status);
    }];
    
}

#pragma mark - AFnetworking manager getter

- (AFHTTPRequestOperationManager *) manager {
    
    if (!_manager) {
        
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json",@"application/json", nil];
        
        // 设置超时时间
        [_manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        _manager.requestSerializer.timeoutInterval = 15.f;
        [_manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        
    }
    return _manager;
}

-(void)getNetworkRequestURLString:(NSString *) urlString  andParas:(id) params andTransferGottenData:(transferValue) transfer
{
    [self.manager GET:urlString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        transfer(dic,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        transfer(nil,error);
    }];
    
}

-(void)postNetworkRequestURLString:(NSString *) urlString  andParas:(id) params andTransferGottenData:(transferValue) transfer
{
    [self.manager POST:urlString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        transfer(dic,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        transfer(nil,error);
        
        
    }];
}

- (void)requestWithMethod:(HTTPMethod)method
                 WithPath:(NSString *)path
               WithParams:(NSDictionary*)params
         WithSuccessBlock:(requestSuccessBlock)success
          WithFailurBlock:(requestFailureBlock)failure
{
    switch (method) {
        case GET:
        {
            [self.manager GET:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
                success(dic);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                failure(error);
            }];
            break;
        }
        case POST:
        {
            [self.manager POST:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
                success(dic);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                failure(error);
            }];
            break;
        }
        default:
            break;
    }
}


- (AFHTTPRequestOperation*)getNetworkRequestURLString:(NSString *)url_path parameters:(id)parameters success:(void (^)(id obj))success fail:(void (^)(NSError *error))fail
{
    NSString* urlStr = url_path;
    __block AFHTTPRequestOperation* http_operation;
    AFHTTPRequestOperation* operation =
    [self.manager GET:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
            http_operation = operation;
            success(dic);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail) {
            http_operation = operation;
            fail(error);
        }
    }];
    
    return operation;
}

- (AFHTTPRequestOperation*)postNetworkRequestURLString:(NSString *)url_path parameters:(id)parameters success:(void (^)(id obj))success fail:(void (^)(NSError *error))fail
{
    NSString* urlStr = url_path;
    __block AFHTTPRequestOperation* http_operation;
    AFHTTPRequestOperation* operation =
    [self.manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
            http_operation = operation;
            success(dic);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (fail) {
            http_operation = operation;
            
            NSLog(@"failfail%@",fail);
            fail(error);
        }
    }];
    
    return operation;
    
}

- (AFHTTPRequestOperation *)networkrequestWithMethod:(HTTPMethod)method
                                            WithPath:(NSString *)path
                                          WithParams:(NSDictionary*)params
                                    WithSuccessBlock:(requestSuccessBlock)success
                                     WithFailurBlock:(requestFailureBlock)failure
{
    NSString* urlStr = path;
    __block AFHTTPRequestOperation* http_operation;
    if (method==GET) {
        AFHTTPRequestOperation* operation =
        [self.manager GET:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
            http_operation = operation;
            success(dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            http_operation = operation;
            failure(error);
        }];
        
        return operation;

    }
    else if(method == POST)
    {
        AFHTTPRequestOperation* operation =
        [self.manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
            http_operation = operation;
            success(dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            http_operation = operation;
            failure(error);
        }];
        return operation;
    }
    return nil;
    
}



@end
